<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>应付记录列表</title>
    <script type="text/javascript">
      var newPayableWin;
      var editPayableWin;
      var payableId;
      var payableAuditWin;
      var payableAuditForCancelWin;
      Ext.onReady(function(){

        var vendor =  Ext.create('Ext.form.ComboBox', {
          fieldLabel: '供应商',
          store: vendorStore,
          valueField: 'id',
          displayField: 'name',
          name: 'vendor_id',          
          loadingText: '搜索中...',
          //pageSize: 10,          
          minChars: 0,          
          editable:false,
          anchor: '90%'          
        });

        var beginDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '付款日期',
          name: 'begin_date',
          id: 'begin_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          vtype: 'daterange',
          endDateField: 'end_date',
          anchor: '100%'
        });
        
        var endDate = Ext.create('Ext.form.field.Date', {
          fieldLabel: '至    ',labelWidth:30,
          name: 'end_date',
          id: 'end_date',
          format: 'Y-m-d', //格式化日期
          //disabledDays: [0,6],	//不让选择周六，周日
          //disabledDaysText: "周末要休息",
          invalidText: "您输入的日期格式不对，正确格式为2010-01-01",          
          vtype: 'daterange',
          startDateField: 'begin_date',
          anchor: '100%'
        });


        var searchButton = Ext.create('Ext.Button', {
          text: '搜索',
          iconCls: 'finding',
          width:70,
          handler: function(){
            payableDS.removeAll();
            payableDS.load({params:{
                'vendor_id':vendor.getValue(),
                'begin_date':beginDate.getRawValue(),
                'end_date':endDate.getRawValue()
              }});
          }
        });

        var clearButton = Ext.create('Ext.Button', {
          text: '清空',
          width:50,
          handler: function(){
            queryFormPanel.form.reset();
          }
        });

        var queryFormPanel = Ext.create('Ext.form.Panel', {
          fieldDefaults: {
            labelWidth: 60,
            labelAlign: 'right'
          },
          width: screenWidth-165,
          frame : true,
          border:false,
          items: [{
              xtype: 'fieldset',
              defaults: {width: screenWidth-290},
              title: '查询条件',
              border: true,
              items: [{
                  layout: 'column',
                  xtype: 'container',
                  items: [{
                      columnWidth: .25,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [vendor]
                    }, {
                      columnWidth: .13,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [beginDate]
                    }, {
                      columnWidth: .13,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [endDate]
                    },{
                      columnWidth: .1,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [searchButton]
                    }, {
                      columnWidth: .1,
                      layout: 'anchor',
                      xtype: 'container',
                      items: [clearButton]
                    }]
                }]
            }]
        });
        //----------------
        Ext.define('PayableModel', {
          extend : 'Ext.data.Model',
          fields : [{name: 'id'},
            {
              name: 'amount',
              type: 'float'
            },{
              name: 'new_amount',
              type: 'float'
            }, {
              name: 'action_date'
            },{
              name: 'type_of_payment'
            }, {
              name: 'description'
            }, {
              name: 'auditing_status'
            },{
              name:'audit_opinion'
            }, {
              name: 'auditor_name',
              mapping:'auditor',
              convert:function(value,record){
                if(value == ""){
                  return "";
                }else{
                  return value.name;
                }
              }
            }, {
              name: 'vendor_name',
              mapping:'vendor.name'
            }, {
              name: 'employee_name',
              mapping:'employee.name'
            }, {
              name: 'purchase_order_number',
              mapping:'purchase_order.order_number'
            }, {
              name: 'sale_auditing_status',
              mapping:'purchase_order.auditing_status'
            }, {
              name: 'created_at'
            }, {
              name: 'updated_at'
            }
          ]
        });

        var payableDS = Ext.create('Ext.data.Store', {
          //autoDestroy : true,
          model : 'PayableModel',
          proxy : {
            type : 'ajax',
            url : '/payables/getAllPayables',
            reader : {
              type : 'json',
              root : 'root',
              totalProperty : 'totalProperty'// 数据集记录总数
            }
          }
        });

        //生成要订单的产品的Grid
        var payableGrid = Ext.create('Ext.grid.Panel', {
          title:'付款记录',
          store: payableDS,
          columns : [Ext.create('Ext.grid.RowNumberer'),{
              header: 'DB',
              dataIndex: 'id',
              width: 50
            },{
              header: '销售订单单号',
              dataIndex: 'purchase_order_number',
              width: 210
            },{
              header: '销售订单状态',
              dataIndex: 'sale_auditing_status',
              width: 90,
              renderer: auditingStatusRender
            },{
              header: '供应商名称',
              dataIndex: 'vendor_name',
              width: 170
            },{
              header: '付款金额',
              dataIndex: 'amount',
              width: 60
            },{
              header: '申请变更付款金额',
              dataIndex: 'new_amount',
              width: 110
            },{
              header: '支付方式',
              dataIndex: 'type_of_payment',
              width: 60
            },{
              header: '付款人',
              dataIndex: 'employee_name',
              width: 60
            },{
              header: '付款日期',
              dataIndex: 'action_date',
              renderer: Ext.util.Format.dateRenderer('Y-m-d'),
              width: 75
            },{
              header: '描述',
              dataIndex: 'description',
              width: 180
            },{
              header: '审批状态',
              dataIndex: 'auditing_status',
              width: 70,
              renderer: auditingStatusRender
            },{
              header: '审批意见',
              dataIndex: 'audit_opinion',
              width: 70
            },{
              header: '审批者',
              dataIndex: 'auditor_name',
              width: 55
            },{
              header: '生成日期',
              dataIndex: 'created_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width: 115
            },{
              header: '修改日期',
              dataIndex: 'updated_at',
              renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),width: 115
            }],
          selModel :Ext.create('Ext.selection.CheckboxModel'),
          loadMask: true,
          width: screenWidth-165,
          height: 570,
          frame: true,
          bbar: Ext.create('Ext.toolbar.Paging', {
            pageSize: pageSize + 10,
            store: payableDS,
            displayInfo: true,
            displayMsg: "显示第 {0} 条到 {1} 条记录，一共 {2} 条",
            emptyMsg: "没有记录"
          })
        });

        payableGrid.on('itemcontextmenu', function(grid, record,item,index,e,options){
          payableId = payableDS.getAt(index).get('id');
          var auditingStatus = payableDS.getAt(index).get('auditing_status');
          auditForAdd.setDisabled(true);
          auditForCancel.setDisabled(true);
          if (auditingStatus.indexOf('申请新增') > -1 || auditingStatus.indexOf('申请变更') > -1){
            auditForAdd.setDisabled(false);
          }
          if (auditingStatus.indexOf('申请撤消') > -1) {
            auditForCancel.setDisabled(false);
          }
          e.preventDefault();
          rightClick.showAt(e.getXY());
        });
        //############ 订单-右键菜单 ##########
        var auditForAdd = Ext.create('Ext.menu.Item', {
          text: '【审批付款】',
          //iconCls: 'album',
          handler: rightMenuAuditFn
        });

        var auditForCancel = Ext.create('Ext.menu.Item', {
          text: '【审批付款  <em style=color:red>(撤消)</em>】',
          //iconCls: 'album',
          handler: rightMenuAuditForCancelFn
        });

        function rightMenuAuditFn(){
          payableAuditWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 307,
            height: 217,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '付款审批',
            autoLoad: {
              url: "/payables/auditPayable?id="+payableId,
              scripts: true
            }
          });
          payableAuditWin.on("destroy",function(){
            payableDS.load();
          });
          payableAuditWin.resizable = false;
          payableAuditWin.show();
        }

        function rightMenuAuditForCancelFn(){
          payableAuditForCancelWin = Ext.create('Ext.window.Window', {
            layout: 'fit',
            width: 305,
            height: 190,
            closeAction: 'destroy',
            plain: true,
            modal: true,
            constrain:true,
            //modal: true,
            title: '采购订单审批<em style=color:red>(撤消)</em>',
            autoLoad: {
              url: "/payables/auditPayableForCancel?id="+payableId,
              scripts: true
            }
          });
          payableAuditForCancelWin.on("destroy",function(){
            payableDS.load();
          });
          payableAuditForCancelWin.show();
        }
        
        var rightClick = Ext.create('Ext.menu.Menu', {
          items: [auditForAdd,auditForCancel]
        });
        
        //总的panel
        var workbenchPanel = Ext.create('Ext.form.Panel', {
          width: screenWidth-165,
          height: 650,
          frame:false,
          
          layout: {type: 'table',columns:1},
          
          items:[queryFormPanel,payableGrid]
        });
        workbenchPanel.render("payables_workbench_panel");
      })
    </script>
  </head>
  <body>
    <div id="payables_workbench_panel"></div>
  </body>
</html>